package com.pj.point.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.pj.point.util.Util;

public class PaintView extends View {

    public Paint paint;
    public Bitmap bitmap;
    public Canvas myCanvas;
    public Path path;
    public Mode myMode = Mode.DRAW;
    public Xfermode clearMode;


    public PaintView(Context context) {
        super(context);
    }

    public PaintView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        int width = context.getResources().getDisplayMetrics().widthPixels;
        int height = context.getResources().getDisplayMetrics().heightPixels;
        bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        myCanvas = new Canvas(bitmap);
        myCanvas.drawColor(Color.WHITE);


        path = new Path();

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);

        clearMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);


    }

    public PaintView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PaintView(Context context,  AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    public void setMode(Mode mode) {
        if (myMode != mode) {
            myMode = mode;
            if (myMode == Mode.DRAW) {
                paint.setXfermode(null);
                paint.setStrokeWidth(20);
            } else {
                paint.setXfermode(clearMode);
                paint.setStrokeWidth(20);
            }
        }
    }

    public String save(){
         return Util.saveBitmap(getContext(),bitmap);
    }

    public String saveByUri(){
        return Util.save(getContext(),bitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,0,0,paint);
        canvas.drawPath(path,paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(x,y);
                break;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(x,y);
                myCanvas.drawPath(path,paint);
                break;
            case MotionEvent.ACTION_UP:
                myCanvas.drawPath(path,paint);
                path.reset();
                break;
        }
        invalidate();
        return true;
    }

    public enum Mode {
        DRAW,
        ERASER
    }

}
